<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateKfUserRoleMappings extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        /**
         * 权限关联
         * 如果需要定位某人的具体角色 userId + 品牌ID/门店ID + type
         */
        Schema::create('user_role_mappings', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger("userId")->comment("用户ID")->default(0);
            $table->unsignedInteger("roleId")->comment("角色ID")->default(0);
            $table->unsignedInteger("brandId")->comment("品牌ID")->default(0);
            $table->unsignedInteger("shopId")->comment("店铺ID")->default(0);

            $table->unsignedInteger('type')->default(0)->comment("默认0 1web 2pos");
            $table->unsignedInteger('isDeleted')->default(0)->comment("是否删除 0：未删除，1：已删除");
            $table->unsignedInteger("createdAt")->default(0)->comment("创建时间");
            $table->unsignedInteger("updatedAt")->default(0)->comment("修改时间");

            $table->index(["userId", "brandId"], "idx_user_brand");
            $table->index(["userId", "shopId"], "idx_user_shop");
            $table->index("createdAt", "idx_created_at");

            $table->collation = 'utf8mb4_general_ci';
        });

        $prefix = config('other.DB_PREFIX', '');
        \Illuminate\Support\Facades\DB::statement("ALTER TABLE `{$prefix}user_role_mappings` COMMENT = '用户角色关联表'"); //表注释
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('user_role_mappings');
    }
}
